using System;
using System.Collections.Generic;
using UnityEngine;
using BgmTypeValue = System.Int32;
// ReSharper disable PartialTypeWithSinglePart

namespace Life.ESound
{
    public class BgmSelectAttribute : PropertyAttribute
    {

    }
    
    public static partial class BgmSoundType
    {
        private static List<Tuple<string, int>> _content;

        private static Dictionary<int, string> _nameMapping;

        private static Dictionary<string, int> _valueMapping;

        public const BgmTypeValue None = 0;

        static BgmSoundType()
        {
            var fields = typeof(BgmSoundType).GetFields();
            _content = new List<Tuple<string, int>>(fields.Length);
            _nameMapping = new Dictionary<int, string>(fields.Length);
            _valueMapping = new Dictionary<string, int>(fields.Length);
            foreach (var field in fields)
            {
                if (field.FieldType == typeof(int))
                {
                    int value = (int)field.GetValue(null);
                    _content.Add(new Tuple<string, int>(field.Name, value));
                    _nameMapping.Add(value, field.Name);
                    _valueMapping.Add(field.Name, value);
                }
            }
            _content.Sort((x, y) => x.Item2.CompareTo(y.Item2));
        }

        public static string GetName(int index)
        {
            return _content[index].Item1;
        }

        public static BgmTypeValue GetValue(int index)
        {
            return _content[index].Item2;
        }

        public static bool ParseToName(BgmTypeValue value, out string name)
        {
            return _nameMapping.TryGetValue(value, out name);
        }

        public static bool ParseToValue(string name, out BgmTypeValue value)
        {
            return _valueMapping.TryGetValue(name, out value);
        }

        public static int Length => _content.Count;
    }
}
